﻿using Dora.Tools.Insurance.AM.Dto;
using Dora.Tools.Insurance.AYB.Dto;
using Dora.Tools.Utility;
using Dora.Tools.Utility.Const.ApiSetting;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Dora.Tools.Insurance.AYB
{
    /// <summary>
    /// 安馒接口对接帮助类
    /// </summary>
    public class AMInsuranceHelper
    {
        /// <summary>
        /// 投保交易
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static AMApplyResponseDto Apply(string pwd, AMApplyRequestDto data)
        {
            string json = JsonConvert.SerializeObject(data);
            string sign = CreateSign(pwd, json);
            RestRequest request = new RestRequest("/eservice/apply", Method.POST);
            request.AddQueryParameter("sign", sign);
            request.AddQueryParameter("data", json);

            var dto = RestHelper.Execute<AMApplyResponseDto>(InsuranceConstValue.AM_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 安馒退保请求
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static AMCancelResponseDto Cancel(string pwd, AMCancelRequestDto data)
        {
            string json = JsonConvert.SerializeObject(data);
            string sign = CreateSign(pwd, json);
            RestRequest request = new RestRequest("/eservice/cancel", Method.POST);
            request.AddQueryParameter("sign", sign);
            request.AddQueryParameter("data", json);

            var dto = RestHelper.Execute<AMCancelResponseDto>(InsuranceConstValue.AM_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 对账交易 获取某一个时间区间的所有投保退保动作明细
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static AMCheckResponseDto Check(string pwd, AMCheckRequestDto data)
        {
            string json = JsonConvert.SerializeObject(data);
            string sign = CreateSign(pwd, json);
            RestRequest request = new RestRequest("/eservice/check", Method.POST);
            request.AddQueryParameter("sign", sign);
            request.AddQueryParameter("data", json);

            var dto = RestHelper.Execute<AMCheckResponseDto>(InsuranceConstValue.AM_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 通过接口请求下载
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DownEPolicyResponseDto DownEPolicy(string pwd, DownEPolicyRequestDto data)
        {
            string json = JsonConvert.SerializeObject(data);
            string sign = CreateSign(pwd, json);
            RestRequest request = new RestRequest("/eservice/downEPolicy", Method.POST);
            request.AddQueryParameter("sign", sign);
            request.AddQueryParameter("data", json);

            var dto = RestHelper.Execute<DownEPolicyResponseDto>(InsuranceConstValue.AM_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 查询余额--可用额度查询
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static AMGetBalanceResponseDto GetBalance(string pwd, AMGetBalanceRequestDto data)
        {
            string json = JsonConvert.SerializeObject(data);
            string sign = CreateSign(pwd, json);
            RestRequest request = new RestRequest("/eservice/getBalance", Method.POST);
            request.AddQueryParameter("sign", sign);
            request.AddQueryParameter("data", json);

            var dto = RestHelper.Execute<AMGetBalanceResponseDto>(InsuranceConstValue.AM_BaseUrl, request);
            return dto;
        }

        /// <summary>
        /// 生成请求的签名
        /// </summary>
        /// <param name="pwd"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static string CreateSign(string pwd, string json)
        {
            return DEncryptHelper.GetMD5(json + pwd);
        }
    }
}
